package city.spring.modules.material.controller;

import city.spring.modules.material.entity.MaterialBreakageDeliveryEntity;
import city.spring.modules.material.entity.MaterialreceiptEntity;
import city.spring.modules.material.service.MaterialBreakageDeliveryService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Controller：报损出库
 *
 * @author 谢金婷
 * @date 2021-01-20 10:51:40
 */
@RestController
@RequestMapping("/material_breakage_delivery")
public class MaterialBreakageDeliveryController extends ApiController {
    private final MaterialBreakageDeliveryService materialBreakageDeliveryService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MaterialBreakageDeliveryEntity, ?>> entityOrderFields;

    public MaterialBreakageDeliveryController(MaterialBreakageDeliveryService materialBreakageDeliveryService) {
        this.materialBreakageDeliveryService = materialBreakageDeliveryService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MaterialBreakageDeliveryEntity::getId);
        entityOrderFields.add(MaterialBreakageDeliveryEntity::getBreakageUser);
        entityOrderFields.add(MaterialBreakageDeliveryEntity::getDepartmentId);
        entityOrderFields.add(MaterialBreakageDeliveryEntity::getBreakageDate);
        entityOrderFields.add(MaterialBreakageDeliveryEntity::getReporter);
    }

    /**
     * 获取全部的 <strong>报损出库</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MaterialBreakageDeliveryEntity entity) {
        LambdaQueryChainWrapper<MaterialBreakageDeliveryEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>报损出库</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault.SortDefaults({@SortDefault(sort = "id", direction = Sort.Direction.DESC)}) Pageable pageable, MaterialBreakageDeliveryEntity entity, Date[] breakageDate) {
//        LambdaQueryChainWrapper<MaterialBreakageDeliveryEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<MaterialBreakageDeliveryEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends MaterialBreakageDeliveryEntity> queryWrapper = buildQuery(entity);
        if (breakageDate != null && breakageDate.length == 2) {
            queryWrapper.ge("t1.breakage_date", breakageDate[0]);
            queryWrapper.le("t1.breakage_date", breakageDate[1]);
        }
        return success(materialBreakageDeliveryService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>报损出库</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        QueryWrapper<MaterialBreakageDeliveryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<MaterialBreakageDeliveryEntity> records = materialBreakageDeliveryService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));

//        return success(materialBreakageDeliveryService.getById(id));
    }

    /**
     * 添加一个 <strong>报损出库</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MaterialBreakageDeliveryEntity entity) {
        materialBreakageDeliveryService.saveMaterialBreakageDelivery(entity);

//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
        return success(entity);

    }

    /**
     * 修改一个 <strong>报损出库</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MaterialBreakageDeliveryEntity entity) {
        entity.setId(id);
        materialBreakageDeliveryService.updateMaterialBreakageDelivery(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>报损出库</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        materialBreakageDeliveryService.deleteMaterialBreakageDelivery(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>报损出库</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        materialBreakageDeliveryService.deleteMaterialBreakageDelivery(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MaterialBreakageDeliveryEntity> buildLambdaQuery(MaterialBreakageDeliveryEntity entity) {
        LambdaQueryChainWrapper<MaterialBreakageDeliveryEntity> lambdaQuery = materialBreakageDeliveryService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MaterialBreakageDeliveryEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBreakageUser()), MaterialBreakageDeliveryEntity::getBreakageUser, entity.getBreakageUser());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDepartmentId()), MaterialBreakageDeliveryEntity::getDepartmentId, entity.getDepartmentId());
        lambdaQuery.eq(entity.getBreakageDate() != null, MaterialBreakageDeliveryEntity::getBreakageDate, entity.getBreakageDate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReporter()), MaterialBreakageDeliveryEntity::getReporter, entity.getReporter());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MaterialBreakageDeliveryEntity> buildQuery(MaterialBreakageDeliveryEntity entity) {
        QueryWrapper<MaterialBreakageDeliveryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getBreakageUser()), "t1.breakage_user", entity.getBreakageUser());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "t2.name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getDepartmentId()), "t1.department_id", entity.getDepartmentId());
        wrapper.like(StringUtils.isNotBlank(entity.getDepartmentName()), "t3.title", entity.getDepartmentName());
        wrapper.eq(entity.getBreakageDate() != null, "t1.breakage_date", entity.getBreakageDate());
        wrapper.like(StringUtils.isNotBlank(entity.getReporter()), "t1.reporter", entity.getReporter());
        wrapper.like(StringUtils.isNotBlank(entity.getReporterName()), "t4.name", entity.getReporterName());
        return wrapper;
    }

    /**
     * 报损出库确认
     * @param id
     * @param entity
     * @return
     */
    @PutMapping("/confirmDelivery/{id}")
    public Object confirmDelivery(@PathVariable String id, @RequestBody MaterialBreakageDeliveryEntity entity) {
        materialBreakageDeliveryService.confirmDelivery(id, entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
